home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / icon play / ploticons.c < prev   
Encoding:
C/C++ Source or Header  |  2000-06-23  |  10.8 KB  |  387 lines

  1. /*
  2.     File:        PlotIcons.c
  3.  
  4.     Contains:    snippet to demonstrate the use of the system 7 icon utilities
  5.                 and the system 7 popup control cdef in a program using modal dialogs
  6.     
  7.                 This shows: how to use the sys 7.0 popup
  8.                 and how to plot Icons from Icon families.
  9.  
  10.     Written by: Nick Thompson    
  11.  
  12.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 8/6/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 5/11/94        nt                updated to use std file icons, in another dialog box
  25.                 8/96                        old routines names changed; Str255 theItem, OSErr
  26.                                             theErr and short userSelection removed from 
  27.                                             DoStdFileIconsDialog; line 266 - NewModalFilterProc 
  28.                                             added; fixed return value for OurFilter to eliminate
  29.                                             compiler warnings
  30.  
  31. */
  32. #include <Menus.h>
  33. #include <Fonts.h>
  34. #include <Dialogs.h>
  35. #include <icons.h>
  36.  
  37. // prototypes
  38. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit) ;
  39. void DoStdFileIconsDialog(void);
  40. void UpdateIconsDialog(    DialogPtr thePopupDialog );
  41. void    FlashDialogItem(DialogPtr dlg, short itemToFlash);
  42.  
  43. // constants used in the dialog filter proc
  44. const char kEnter    = 0x03 ;
  45. const char kReturn    = 0x0D ;
  46. const char kEscape    = 0x1B ;
  47. const char kPeriod    = '.' ;
  48.  
  49.  
  50. // constants for our dialog IDs these are used in the filterproc to do the "right thing"
  51. // by calling the appropriate redrawing code
  52.  
  53. const long    kIconsDialog        = 1000 ;
  54. const long    kStdFileIconsDialog    = 1001 ;
  55.  
  56.  
  57. // constants for items in the dialog
  58. enum {
  59.     kThePopupMenu = 4 ,        // popup menu is the fourth item in the dialog
  60.     kUserTextArea = 5 ,        // this is a rect for us to write a string with the user selection
  61.     kLargeIconArea = 8 ,    // rect to draw the lg icon into
  62.     kSmallIconArea = 9 ,    // rect to draw the small icon
  63.     kMoreIconBtn = 10         // we want to see the std file icon dialog
  64. } ;
  65.  
  66. // table to tie up the menu selection with the 
  67. // actual icon IDs defined in Icons.h
  68. // These are documented in the resource manager chapter
  69. // of Inside Macintosh: More Toolbox Essentials, Page 1-130 onwards.
  70.  
  71. long    resIDTable[] = {
  72.         0,                                    /* Dummy for start of the array */
  73.         genericDocumentIconResource,        /* [1]  "Generic Document" */
  74.         genericApplicationIconResource,        /* [2]  "Generic Application" */
  75.         genericDeskAccessoryIconResource,    /* [3]  "Generic Desk Accessory" */
  76.         genericEditionFileIconResource,        /* [4]  "Generic Edition File" */
  77.         genericStationeryIconResource,        /* [5]  "Generic Stationary" */
  78.         genericPreferencesIconResource,        /* [6]  "Generic Preferences" */
  79.         genericQueryDocumentIconResource,    /* [7]  "Generic Query Document" */
  80.         genericExtensionIconResource,        /* [8]  "Generic Extension" */
  81.         genericFolderIconResource,            /* [9]  "Generic Folder" */
  82.         privateFolderIconResource,            /* [10]  "Private Folder" */
  83.         ownedFolderIconResource,            /* [11]  "Owned Folder" */
  84.         dropFolderIconResource,                /* [12]  "Drop Folder" */
  85.         sharedFolderIconResource,            /* [13]  "Shared Folder" */
  86.         mountedFolderIconResource,            /* [14]  "Mounted Folder" */
  87.         systemFolderIconResource,            /* [15]  "System Folder" */
  88.         appleMenuFolderIconResource,        /* [16]  "Apple Menu Folder" */
  89.         startupFolderIconResource,            /* [17]  "Startup Items Folder" */
  90.         controlPanelFolderIconResource,        /* [18]  "Control Panels" */
  91.         printMonitorFolderIconResource,        /* [19]  "Printmonitor Folder" */
  92.         preferencesFolderIconResource,        /* [20]  "Preferences Folder" */
  93.         extensionsFolderIconResource,        /* [21]  "Extensions" */
  94.         -3968,                                /* [22]  "Fonts" */
  95.         floppyIconResource,                    /* [23]  "Floppy Icon" */
  96.         trashIconResource,                    /* [24]  "Trash Icon" */
  97.         fullTrashIconResource                /* [25]  "Full Trash Icon" */
  98. } ;
  99.  
  100. long    stdFileResTablep[] = {
  101.         1,
  102.         2,
  103.         3,
  104.         4,
  105.         5
  106. } ;
  107.  
  108.  
  109.  
  110. // Structure for the private data for a popup control.
  111. // This structure is documented on page 5-77 
  112. // Inside Macintosh: Macintosh Toolbox Essentials
  113.  
  114. typedef struct popupPrivateData {
  115.     MenuHandle     mHandle;     // the popup menu handle 
  116.     short         mID;        // the popup menu ID 
  117.     // after these two public fields is the mPrivate private data, 
  118.     // which may be any old size and should not be messed with 
  119. }    popupPrivateData;
  120.  
  121.  
  122. void DoIconsDialog(void) ;
  123.  
  124.  
  125. void main(void) {
  126.  
  127.     // initialize the toolbox
  128.     InitGraf(&qd.thePort); InitFonts(); InitWindows(); InitMenus();
  129.     TEInit(); InitDialogs(nil); InitCursor(); FlushEvents(everyEvent,0);
  130.     
  131.     
  132.     DoIconsDialog() ;
  133. }
  134.  
  135.  
  136. void DoStdFileIconsDialog(void) 
  137. {
  138.     popupPrivateData    **myPopupPrivateDataPtr ;
  139.     
  140.     short                iKind;
  141.     Handle                iHandle;
  142.     Rect                iRect;
  143.     
  144.     MenuHandle            thePopupMenuHdl ;
  145.     
  146.     DialogPtr             thePopupDialog ;
  147.     short                itemHit ;
  148.     GrafPtr                savedPort ;
  149.     
  150.     short                index ;
  151.  
  152.     thePopupDialog = GetNewDialog ( 129, nil, (WindowPtr)-1 );
  153.     SetWRefCon((WindowPtr)thePopupDialog, kStdFileIconsDialog ) ;
  154.     GetPort( &savedPort ) ;
  155.     SetPort( thePopupDialog ) ;
  156.  
  157.  
  158.     // get a reference to the popup control, this is item 4 in the dialog
  159.     // we need to get the menuhandle associated with the control, it is in the private
  160.     // control data field, as documented in Inside Macintosh: Toolbox page 5-77
  161.     
  162.     // get the control handle for the popup            
  163.     GetDialogItem ( thePopupDialog, kThePopupMenu, &iKind, &iHandle, &iRect) ;
  164.     
  165.     // extract from the control the menuhandle
  166.     myPopupPrivateDataPtr = (popupPrivateData **)(**(ControlHandle)iHandle).contrlData ; 
  167.     thePopupMenuHdl = (**myPopupPrivateDataPtr).mHandle ;
  168.     
  169.     // set up the Icons - we have 5 items here
  170.     for( index = 0;    index < 5; index++ ) 
  171.         SetItemIcon( thePopupMenuHdl, index + 1, stdFileResTablep[index]) ;
  172.     do {
  173.     
  174.         ModalDialog ( NewModalFilterProc(OurFilter), &itemHit );
  175.         
  176.         switch ( itemHit ) {
  177.         
  178.             default :
  179.                 break ;        
  180.         }
  181.         
  182.     } while( itemHit != ok ) ;
  183.     
  184.     DisposeDialog ( thePopupDialog );
  185.     SetPort( savedPort ) ;
  186.  
  187. }
  188.  
  189.  
  190.  
  191. // this function ensures that the correct Icons are drawn in the dialog
  192. // corresponding with the current user selection.
  193.  
  194. void UpdateIconsDialog(    DialogPtr thePopupDialog )     // dialog pointer for the popup dialog
  195. {
  196.     popupPrivateData    **myPopupPrivateDataPtr ;
  197.     
  198.     short                iKind;
  199.     Handle                iHandle;
  200.     Rect                iRect;
  201.     
  202.     MenuHandle            thePopupMenuHdl ;
  203.     Str255                theItem ;
  204.  
  205.     OSErr                theErr ;
  206.     
  207.     
  208.     short                userSelection ;            // the current menu selection
  209.     
  210.     // the user choose the popup.  The item number selected will be the control value
  211.     // we need to get the menuhandle associated with the control, it is in the private
  212.     // control data field, as documented in Inside Macintosh: Toolbox page 5-77
  213.     
  214.     // get the control handle for the popup            
  215.     GetDialogItem ( thePopupDialog, kThePopupMenu, &iKind, &iHandle, &iRect) ;
  216.     
  217.     // extract from the control the menuhandle
  218.     myPopupPrivateDataPtr = (popupPrivateData **)(**(ControlHandle)iHandle).contrlData ; 
  219.     thePopupMenuHdl = (**myPopupPrivateDataPtr).mHandle ;
  220.  
  221.     // get the string associated with the users selection
  222.     userSelection = GetControlValue((ControlHandle)iHandle) ;
  223.     GetMenuItemText ( thePopupMenuHdl, userSelection, theItem );
  224.     
  225.     // get the rect we are drawing in
  226.     GetDialogItem ( thePopupDialog, kUserTextArea, &iKind, &iHandle, &iRect) ;
  227.     
  228.     // erase the area to draw in
  229.     EraseRect( &iRect ) ;
  230.                 
  231.     // and draw the current user selection
  232.     MoveTo(iRect.left + 1, iRect.bottom - 6) ;
  233.     DrawString( theItem ) ;
  234.     
  235.     // now plot the Icons.  The id of the item selected in
  236.     // the menu will be in userSelection.
  237.     
  238.     // first draw the large icon
  239.     
  240.     // get the rect we are drawing in
  241.     GetDialogItem ( thePopupDialog, kLargeIconArea, &iKind, &iHandle, &iRect) ;
  242.  
  243.     // erase the area to draw in
  244.     EraseRect( &iRect ) ;
  245.  
  246.     theErr = PlotIconID( &iRect, atVerticalCenter, ttNone, resIDTable[ userSelection ] ) ;
  247.     
  248.     // now draw the small icon
  249.     
  250.     // get the rect we are drawing in
  251.     GetDialogItem ( thePopupDialog, kSmallIconArea, &iKind, &iHandle, &iRect) ;
  252.  
  253.     // erase the area to draw in
  254.     EraseRect( &iRect ) ;
  255.  
  256.     theErr = PlotIconID( &iRect, atVerticalCenter, ttNone, resIDTable[ userSelection ] ) ;
  257.  
  258.  
  259.  
  260. }
  261.  
  262.  
  263. void DoIconsDialog(void) 
  264. {
  265.     DialogPtr             thePopupDialog ;
  266.     short                itemHit ;
  267.     GrafPtr                savedPort ;
  268.     ModalFilterUPP        modalFilterUPP;
  269.  
  270.     modalFilterUPP = NewModalFilterProc(OurFilter);
  271.     if (modalFilterUPP)
  272.     {
  273.         thePopupDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  274.         if (thePopupDialog)
  275.         {
  276.             SetWRefCon((WindowPtr)thePopupDialog, kIconsDialog ) ;
  277.             GetPort( &savedPort ) ;
  278.             SetPort( thePopupDialog ) ;
  279.             UpdateIconsDialog( thePopupDialog ) ;
  280.  
  281.  
  282.             // get a reference to the popup control, this is item 4 in the dialog
  283.             
  284.             do {
  285.             
  286.                 ModalDialog ( modalFilterUPP, &itemHit );
  287.                 
  288.                 switch ( itemHit ) {
  289.                 
  290.                     case kThePopupMenu :
  291.                     
  292.                         UpdateIconsDialog( thePopupDialog ) ;
  293.                         // reset itemHit to something else or we'll continually redraw
  294.                         itemHit = 0 ;
  295.                         break ;
  296.                         
  297.                     case kMoreIconBtn :
  298.                         
  299.                         SetPort( savedPort ) ;
  300.                         DoStdFileIconsDialog() ;
  301.                         SetPort( thePopupDialog ) ;
  302.                         break ;
  303.                 
  304.                 }
  305.                 
  306.             } while( itemHit != ok ) ;
  307.             
  308.             DisposeDialog ( thePopupDialog );
  309.             SetPort( savedPort ) ;
  310.         }
  311.         DisposeRoutineDescriptor (modalFilterUPP);
  312.     }
  313. }
  314.  
  315.  
  316. void    FlashDialogItem(DialogPtr dlg, short itemToFlash)
  317. {
  318.     short    iKind;
  319.     Handle    iHandle;
  320.     Rect    iRect;
  321.     unsigned long    ignored;
  322.  
  323.     GetDialogItem(dlg, itemToFlash, &iKind, &iHandle, &iRect);
  324.     HiliteControl((ControlHandle) iHandle, 1);
  325.     Delay(8, &ignored);
  326.     HiliteControl((ControlHandle) iHandle, 0);
  327. }
  328.  
  329.  
  330.  
  331.  
  332. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit)
  333. {
  334.     Boolean    result = false;
  335.     short    iKind;
  336.     Handle    iHandle;
  337.     Rect    iRect;
  338.     char    key;
  339.     short    radius;
  340.  
  341.     switch (event->what) {
  342.  
  343.         case keyDown:
  344.         case autoKey:
  345.             key = event->message & charCodeMask;
  346.             if (event->modifiers & cmdKey) {        /* Command key down */
  347.                 if (key == kPeriod) {
  348.                     *itemHit = ok;
  349.                     FlashDialogItem(dlg, *itemHit);
  350.                 }
  351.                 result = true;            /* This says we handle ALL command keys */
  352.             } else {
  353.                 if ((key == kReturn) || (key == kEnter)) {
  354.                     *itemHit = ok;
  355.                     FlashDialogItem(dlg, *itemHit);
  356.                     result = true;
  357.                 }
  358.                 else if (key == kEscape) {
  359.                     *itemHit = ok;
  360.                     FlashDialogItem(dlg, *itemHit);
  361.                     result = true;
  362.                 }
  363.             }
  364.             break;
  365.  
  366.         case updateEvt:
  367.             SetPort(dlg);
  368.                 
  369.             GetDialogItem(dlg, ok, &iKind, &iHandle, &iRect);            
  370.             InsetRect(&iRect, -4, -4);
  371.             radius = (iRect.bottom - iRect.top) / 2;
  372.             if (radius < 16)
  373.                 radius = 16;
  374.                 
  375.             PenSize(3,3);
  376.             FrameRoundRect(&iRect, radius, radius);
  377.             PenNormal();
  378.             
  379.             if(    GetWRefCon((WindowPtr)dlg ) == kIconsDialog ) 
  380.                 UpdateIconsDialog( dlg ) ;
  381.             break;
  382.     }
  383.  
  384.     return result;
  385. }
  386.  
  387.